//
//  CustomBtn.m
//  不同位置响应
//
//  Created by MyMac on 15/9/28.
//  Copyright (c) 2015年 tongfang. All rights reserved.
//

#import "CustomBtn.h"

@implementation CustomBtn


- (void)drawRect:(CGRect)rect {
    CGContextRef ctf=UIGraphicsGetCurrentContext();
    UIImage*image=[self clipImageWithSize:rect.size];
    
    CGContextClipToMask(ctf, rect, image.CGImage);
    
    CGContextStrokePath(ctf);
    const CGFloat components[20]={0.0,0.4,0.8,0.8,0.0,0.7,0.5,0.4,0.0,1.0,1.0,1.0,0.0,0.9,0.9,0.9,0.1,0.2,0.3,1.0};
 CGColorSpaceRef colorSpace= CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradien=CGGradientCreateWithColorComponents(colorSpace, components, NULL, 5);
    CGContextDrawLinearGradient(ctf, gradien, CGPointMake(0, 0), CGPointMake(rect.size.width, rect.size.height), kCGGradientDrawsBeforeStartLocation);
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradien);
   // CGContextDrawImage(ctf, rect, [UIImage imageNamed:@"屏幕快照 2015-09-28 下午5.10.43"].CGImage);
}
-(UIImage*)clipImageWithSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGContextRef ctf=UIGraphicsGetCurrentContext();
    //CGContextSetFillColorWithColor(ctf, [UIColor clearColor].CGColor);
    
    CGContextMoveToPoint(ctf, 0, 0);
    CGContextAddLineToPoint(ctf, 0, size.height);
    //CGContextMoveToPoint(ctf,self.startPoint.x,self.startPoint.y);
    CGContextAddLineToPoint(ctf, self.endPoint.x, self.endPoint.y);
    CGContextAddLineToPoint(ctf, self.startPoint.x, self.startPoint.y);
    CGContextClosePath(ctf);
    CGContextSetFillColorWithColor(ctf, [UIColor blackColor].CGColor);
    CGContextFillPath(ctf);
    UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;



}

@end
